﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Domain.Interface;
using Domain.Model;
using Domain.Model.Domain_MoBan_Model;
using Newtonsoft.Json;
using WebAPI.Attributes;
using Ds.DataBase.Model;

namespace WebAPI.Controllers
{
    public class MoBanController : ApiController
    {
        public IMoBan _imoban { set; get; }
        public IJiaoZhiGong _IJiaoZhiGong { set; get; }
        [HttpPost]
        public string SaveGZJH([FromBody] Domain_GongZuoJiHua model)
        {
            //var result = JsonConvert.DeserializeObject<dynamic>(str);
            var str = JsonConvert.SerializeObject(model);
            if (model.mbid == 0)
            {
                _imoban.AddGongZuoJiHua(str);
            }
            else
            {
                _imoban.UpdateGongZuoJiHua(str);
            }
            return str;
        }
        [HttpGet]
        public void DelMoBan(int mbid, int mblx)
        {
            _imoban.DelMoBan(mbid, mblx);
        }
        [HttpGet]
        public List<Domain_GongZuoJiHua> GetGZJHMB()
        {
            var str = _imoban.GetGongZuoJiHuas();
            return JsonConvert.DeserializeObject<List<Domain_GongZuoJiHua>>(str);
        }

        [HttpGet]
        public Domain_GongZuoJiHua GetGongZuoJiHuaInfo(int mbid)
        {
            var str = _imoban.GetGongZuoJiHua(mbid);
            return JsonConvert.DeserializeObject<Domain_GongZuoJiHua>(str);
        }
        [HttpGet]
        public Domain_JiXiao GetJiXiaoInfo(int mbid)
        {
            var str = _imoban.GetJiXiao(mbid);
            return JsonConvert.DeserializeObject<Domain_JiXiao>(str);
        }
        [HttpGet]
        public List<Domain_JiXiao> GetJiXiaos()
        {
            var str = _imoban.GetJiXiaos();
            return JsonConvert.DeserializeObject<List<Domain_JiXiao>>(str);
        }
        [HttpPost, WriteFileExceptionAspect]
        public string SaveJX([FromBody] Domain_JiXiao model)
        {
            var str = JsonConvert.SerializeObject(model);
            _imoban.AddJiXiao(str);
            return str;
        }
        #region 考核范围_科室

        [HttpGet]
        public List<Domain_KeShi> GetMoBanKaoHeFanWei(int nrid, string username)
        {
            string str = _imoban.GetMoBanKaoHeFanWei(nrid, username);
            return JsonConvert.DeserializeObject<List<Domain_KeShi>>(str);
        }





        #endregion
        #region 考核范围_人员
        [HttpGet]
        public List<Domain_JiaoZhiGong> GetMoBanKaoHeFanWeiRenYuan(int nrid, string username)
        {
            string str = _imoban.GetMoBanKaoHeFanWeiRenYuan(nrid, username);
            return JsonConvert.DeserializeObject<List<Domain_JiaoZhiGong>>(str);
        }

        [HttpGet]
        public void AddKaoHeFanWeiRenYuan(int nrid, string ry, string username)
        {
            _imoban.AddKaoHeFanWeiRenYuan(nrid, ry, username);
        }

        [HttpGet]
        public void DelKaoHeFanWeiRenYuan(int nrid, string rys, string username)
        {
            _imoban.DelKaoHeFanWeiRenYuan(nrid, rys, username);
        }
        #endregion

        #region 新模板
        [HttpGet, WriteFileExceptionAspect]
        public List<Domain_MoBan_Control> GetControlAll()
        {
            if (_imoban == null)
            {
                throw new Exception("AutoFac掉了");
            }
            string result = _imoban.GetControlAll();
            if (string.IsNullOrEmpty(result))
            {
                return new List<Domain_MoBan_Control>();
            }
            return JsonConvert.DeserializeObject<List<Domain_MoBan_Control>>(result);
        }
        [HttpPost]
        public int AddMoBanAnswer(dynamic paras)
        {
            string str = JsonConvert.SerializeObject(paras.model);
            return _imoban.AddMoBanAnswer(int.Parse(paras.mbid.ToString()), int.Parse(paras.ljid.ToString()), int.Parse(paras.nrid.ToString()), str);
        }

        [HttpPost]
        public void UpdateMoBanAnswer(dynamic paras)
        {
            string str = JsonConvert.SerializeObject(paras.model);
            _imoban.UpdateMoBanAnswer(int.Parse(paras.mbid.ToString()), int.Parse(paras.ljid.ToString()), int.Parse(paras.nrid.ToString()), str);
        }
        [HttpGet]
        public void DeleteMoBanAnswer(int mbid, int ljid, int nrid, int answerid)
        {
            _imoban.DeleteMoBanAnswer(mbid, ljid, nrid, answerid);
        }
        [HttpPost, WriteFileExceptionAspect]
        public void AddMoBan(dynamic mb)
        {
            _imoban.AddMoBan(mb.name.ToString(), mb.cjrid.ToString(), int.Parse(mb.mbtype.ToString()));
        }
        [HttpPost, WriteFileExceptionAspect]
        public void UpdateMoBan(int mbid, string name)
        {
            _imoban.UpdateMoBan(mbid, name);
        }
        [HttpGet]
        public void DelMoBanMain(int mbid)
        {
            _imoban.DelMoBan(mbid);
        }
        [HttpPost, WriteFileExceptionAspect]
        public List<Domain_MoBan_Main> GetAllMoBan()
        {
            var result = _imoban.GetAllMoBan();
            if (result == null)
            {
                return new List<Domain_MoBan_Main>();
            }
            //var str = JsonConvert.SerializeObject(result);
            //List<Domain_MoBan_Main> Output = new List<Domain_MoBan_Main>();
            //foreach (var item in result)
            //{
            //    var a = AutoMapper.Mapper.Map<Domain_MoBan_Main>(item);
            //    Domain_MoBan_Main model = new Domain_MoBan_Main();
            //    model.mbid = item.mbid;
            //    model.MoBan_Paths = new List<Domain_MoBan_Path>();
            //    foreach (var item2 in item.MoBan_Paths.ToList())
            //    {
            //        Domain_MoBan_Path model2 = new Domain_MoBan_Path();
            //        model2.ljid = item2.ljid;
            //        foreach (var item3 in item2.MoBan_Contents)
            //        {
            //            Domain_MoBan_Content model3 = new Domain_MoBan_Content();
            //            //model3=item3.
            //        }
            //    }
            //}
            //AuotMapper具体使用方法 将List<StudentOutput>转换为List<StudentOutput>
            //= AutoMapper.Mapper.Map<List<Domain_MoBan_Main>>(result);
            return result;
            //return Json(Output, JsonRequestBehavior.AllowGet);
            //var lst = JsonConvert.DeserializeObject<List<MoBan_Main>>(result);
            //foreach (var item in lst)
            //{
            //    item.XingMing = JsonConvert.DeserializeObject<Domain_JiaoZhiGong>(_IJiaoZhiGong.GetJiaoZhiGongInfo(item.cjr)).XingMing;
            //    item.mbtypename = item.mbtype.ToString();
            //}
            //return lst;
        }
        [HttpPost, WriteFileExceptionAspect]
        public List<Domain_MoBan_Main> GetSearchMoBanList(dynamic keyword)
        {

            string result = _imoban.GetSearchMoBanList(keyword.keyword.ToString());
            return JsonConvert.DeserializeObject<List<Domain_MoBan_Main>>(result);
        }
        [HttpPost, WriteFileExceptionAspect]
        public List<Domain_MoBan_Main> GetExceptMySelfMoBan(dynamic parameters)
        {
            string result = _imoban.GetExceptMySelfMoBan(int.Parse(parameters.mbid.ToString()), parameters.keyword.ToString());
            return JsonConvert.DeserializeObject<List<Domain_MoBan_Main>>(result);
        }
        [HttpPost, WriteFileExceptionAspect]
        public Domain_MoBan_Main GetMoBanInfo(int mbid)
        {
            string result = _imoban.GetMoBanInfo(mbid);
            return JsonConvert.DeserializeObject<Domain_MoBan_Main>(result);
        }
        [HttpPost, WriteFileExceptionAspect]
        public int AddMoBanPath(dynamic paras)
        {
            var ljid = _imoban.AddMoBanPath(int.Parse(paras.mbid.ToString()), int.Parse(paras.model.shunxu.ToString()), paras.model.ljmc.ToString(), int.Parse(paras.model.pid.ToString()), decimal.Parse(paras.model.fenzhi.ToString()), bool.Parse(paras.model.isedit.ToString()), int.Parse(paras.model.ControlID.ToString()), bool.Parse(paras.model.isinclude.ToString()), int.Parse(paras.model.SonMoBanMain.mbid.ToString()));
            return ljid;
        }
        [HttpPost, WriteFileExceptionAspect]
        public void UpdateMoBanPath(dynamic paras)
        {
            var isinclude = bool.Parse(paras.model.isinclude.ToString());
            var sonmobanmbid = 0;
            if (isinclude)
            {
                sonmobanmbid = int.Parse(paras.model.SonMoBanMain.mbid.ToString());
            }
            _imoban.UpdateMoBanPath(int.Parse(paras.mbid.ToString()), int.Parse(paras.model.ljid.ToString()), int.Parse(paras.model.shunxu.ToString()), paras.model.ljmc.ToString(), decimal.Parse(paras.model.fenzhi.ToString()), bool.Parse(paras.model.isedit.ToString()), int.Parse(paras.model.ControlID.ToString()), isinclude, sonmobanmbid);
        }
        [HttpGet]
        public void DelMoBanPath(int mbid, int ljid)
        {
            _imoban.DelMoBanPath(mbid, ljid);
        }
        [HttpPost, WriteFileExceptionAspect]
        public int AddMoBanContent(dynamic paras)
        {
            return _imoban.AddMoBanContent(int.Parse(paras.mbid.ToString()), int.Parse(paras.ljid.ToString()), int.Parse(paras.model.shunxu.ToString()), paras.model.nr.ToString(), int.Parse(paras.model.fenzhi.ToString()), int.Parse(paras.model.ControlID.ToString()));
        }
        [HttpPost, WriteFileExceptionAspect]
        public void UpdateMoBanContent(dynamic paras)
        {
            _imoban.UpdateMoBanContent(int.Parse(paras.mbid.ToString()), int.Parse(paras.ljid.ToString()), int.Parse(paras.model.nrid.ToString()), int.Parse(paras.model.shunxu.ToString()), paras.model.nr.ToString(), int.Parse(paras.model.fenzhi.ToString()), int.Parse(paras.model.ControlID.ToString()));
        }
        [HttpGet]
        public void DelMoBanContent(int mbid, int ljid, int nrid)
        {
            _imoban.DelMoBanContent(mbid, ljid, nrid);
        }

        [HttpGet, WriteFileExceptionAspect]
        public Dictionary<string, int> GetMoBanType()
        {
            string result = _imoban.GetMoBanType();
            return JsonConvert.DeserializeObject<Dictionary<string, int>>(result);
        }
        #endregion
        #region 考核范围设置
        [HttpGet]
        public int AddKaoHeFanWei(string userinfoid)
        {
            return _imoban.AddKaoHeFanWei(userinfoid);
        }
        [HttpGet]
        public void AddKaoHePath(int khfwryid, int ljid)
        {
            _imoban.AddKaoHePath(khfwryid, ljid);
        }
        [HttpGet]
        public void DelKaoHePath(int khfwryid, int nrid)
        {
            _imoban.DelKaoHePath(khfwryid, nrid);
        }
        [HttpGet]
        public void AddBeiKaoHeJiaoZhiGong(int khfwryid, string userid)
        {
            _imoban.AddBeiKaoHeJiaoZhiGong(khfwryid, userid);
        }
        [HttpGet]
        public void AddBeiKaoHeJiaoZhiGongKeShi(int khfwryid, int ksid)
        {
            _imoban.AddBeiKaoHeJiaoZhiGong(khfwryid, ksid);
        }
        [HttpGet]
        public void DelBeiKaoHeJiaoZhiGong(int khfwryid, string userid)
        {
            _imoban.DelBeiKaoHeJiaoZhiGong(khfwryid, userid);
        }
        [HttpGet]
        public void DelKaoHeFanWei(int khfwryid)
        {
            _imoban.DelKaoHeFanWei(khfwryid);
        }
        [HttpGet]
        public List<dynamic> GetKaoHeFanWeis(string name = "")
        {
            var str = _imoban.GetKaoHeFanWeis(name);
            return JsonConvert.DeserializeObject<List<dynamic>>(str);
        }

        public dynamic GetKaoHeFanWei(int khfwryid)
        {
            var str = _imoban.GetKaoHeFanWei(khfwryid);
            return JsonConvert.DeserializeObject<dynamic>(str);
        }
        #endregion
    }
}
